<?php

namespace App\Traits;

use DB;
trait OauthTrait
{
    protected $dbConnection = 'local_mysql';
    protected $clientInfoTable = 'oauth_client_info';
    protected $oauthCodeTable = 'oauth_code_info';
    protected $oauthTokenTable = 'oauth_access_token';
    protected $oauthIdPhoneTable = 'oauth_id_phone';

    public function oauthClientInfo($data)
    {
        $whereData = [
            'client_id' => $data['client_id'],
        ];
        $result = DB::connection($this->dbConnection)
            ->table($this->clientInfoTable)
            ->where($whereData)
            ->first();
        return $result ? true : false;
    }

    public function addClientCode($data)
    {
        $insertData = [
            'client_id' => $data['client_id'],
            'code' => $this->createCode(),
            'redirect_uri' => $data['redirect_uri'],
        ];
        $result = DB::connection($this->dbConnection)
            ->table($this->oauthCodeTable)
            ->insert($insertData);
        $userIdPhone = [
            'user_id' => $data['user_id'],
            'user_phone' => $data['user_phone'],
            'client_id' => $data['client_id'],
            //'created_at' => date('Y-m-d H:i:s', time()),
        ];
        $userIdPhoneExits = DB::connection($this->dbConnection)
            ->table($this->oauthIdPhoneTable)
            ->where($userIdPhone)
            ->first();
        if (!$userIdPhoneExits) {
            $userIdPhone['created_at'] = date('Y-m-d H:i:s', time());
            DB::connection($this->dbConnection)
                ->table($this->oauthIdPhoneTable)
                ->insert($userIdPhone);
        }

        return $result ? true : false;

    }

    public function createCode()
    {
        return mt_rand(1, 99999999);
    }

    public function createToken()
    {
        return md5(mt_rand(1, 99999999999));
    }

    public function oauthCodeInfo($data)
    {
        $whereData = [
            'code' => $data['code'],
            'redirect_uri' => $data['redirect_uri'],
            'client_id' => $data['client_id'],
            'code_is_used' => 0,
        ];
        $result = DB::connection($this->dbConnection)
            ->table($this->oauthCodeTable)
            ->where($whereData)
            ->first();
        if ($result) {
            $updateData = [
                'code_is_used' => 1,
                'deleted_at' => date('Y-m-d H:i:s', time()),
            ];
            $result = DB::connection($this->dbConnection)
                ->table($this->oauthCodeTable)
                ->where($whereData)
                ->update($updateData);

        }
        return $result ? true : false;

    }

    public function addToken($data)
    {
        $insertData = [
            'client_id' => $data['client_id'],
            'access_token' => $data['access_token'],
            'token_type' => $data['token_type'],
            'expires_in' => $data['expires_in'],
            //'refresh_token' => 'jjj',
            'created_at' => date('Y-m-d H:i:s', time()),
        ];
        DB::connection($this->dbConnection)
            ->table($this->oauthTokenTable)
            ->where('client_id', '=', $data['client_id'])
            ->delete();
        $result = DB::connection($this->dbConnection)
            ->table($this->oauthTokenTable)
            ->insert($insertData);
        return $result ? true : false;
    }

    public function oauthTokenInfo($token)
    {
        $whereData = [
            'access_token' => $token,
        ];
        $result = DB::connection($this->dbConnection)
            ->table($this->oauthTokenTable)
            ->where($whereData)
            ->where('expires_in', '>', time())
            ->first();
        return $result ? $result : false;
    }

    public function getUserIdInfo($userId, $clientId)
    {
        $whereData = [
            'user_id' => $userId,
            'client_id' => $clientId,
        ];
        $result = DB::connection($this->dbConnection)
            ->table($this->oauthIdPhoneTable)
            ->where($whereData)
            ->first();

        return $result ? $result : false;

    }

}
